<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/8/27
 * Time: 9:44
 */

namespace app\macht\controller;


use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserUpdate;
use think\Controller;
use think\Request;
use think\Session;

class CardApi extends MachBase
{
    public function api(Request $request){
        if ($request->isAjax()){
            $param = $request->post();
            $chackresult = $this->chackdata($param);
            if ($chackresult['code'] !== 0){

            }else{
                switch ($param['method']){
                    //会员卡概览列表
                    case "cardsurveylist":

                        break;
                }
            }
        }
    }

    public function chackdata($param){

    }

    //会员卡概览列表
    public function cardsurveylist($param){
        $top_mid = Session::get('login_top_mid');
        unset($param['page']);
        unset($param['card_id']);
        if ($param) {
            if ($param['code'] != ''){
                $map['code'] = array('like', '%' . trim($param['code']) . '%');
            }
            if ($param['mid'] != ''){
                $map['mid'] = $param['mid'];
            }else{
                if($this->get_mer_mid() !== ''){
                    $map['mid'] = ['in',$this->get_mer_mid()];
                }else{
                    $map['mid'] = null;
                }
            }
            if ($param['con_username'] != ''){
                $map['con_username'] = array('like', '%' . trim($param['con_username']) . '%');
            }
            if ($param['min_balance'] !== '') {
                $min_balance = $param['min_balance'];
            } else {
                $min_balance = 0;
            }
            if ($param['max_balance'] !== '') {
                $map['add_balance'] = array('between', "" . $min_balance . "," . $param['max_balance'] . "");
            } else {
                $map['add_balance'] = array('egt', $param['min_balance']);
            }
            if ($param['starttime'] !== '') {
                $starttime = strtotime($param['starttime']);
            } else {
                $starttime = 0;
            }
            if ($param['endtime'] !== '') {
                $endtime = strtotime($param['endtime']) + 86400;
            } else {
                $endtime = strtotime(date('Y-m-d H:i:s'));
            }
            $map['create_time'] = array('between', '' . $starttime . ',' . $endtime . '');
        } else {
            $param['max_balance'] = '';
            $param['min_balance'] = '';
            $param['code'] = '';
            $param['mid'] = '';
            $param['con_username'] = '';
            $param['starttime'] = '';
            $param['endtime'] = '';
            if($this->get_mer_mid() !== ''){
                $map['mid'] = ['in',$this->get_mer_mid()];
            }else{
                $map['mid'] = null;
            }
        }
        if (empty($card_id)){
        }else{
            $map['card_id'] = ['eq',$card_id];
        }
        if($this->get_mer_mid() !== ''){
            $mid = ['in',$this->get_mer_mid()];
        }else{
            $mid = null;
        }
        $add_balance['mid'] = $mid;
        $add_balance['add_balance & pay_balance'] = ['neq',0];
        $add_balance['del_balance'] = 0;
        $CardUserUpdate = new CardUserUpdate();
        $CardUserGet = new CardUserGet();
        $survey['total_balance'] = $CardUserUpdate->where($add_balance)->sum('add_balance');     //总金额
        $del_balance['mid'] = $mid;
        $del_balance['type'] = 2;
        $del_balance['del_balance'] = ['>',0];
        $del_balance['cate'] = ['in',[2,3]];;
        $survey['consum_balance'] = $CardUserUpdate->where($del_balance)->sum('del_balance');       //消费额

        $survey['surplus_balance'] = $survey['total_balance'] - $survey['consum_balance'];                          //剩余额度
        $maps['card_id'] = $this->get_card_id();
        $survey['surplus_bonus'] = $CardUserGet->where($maps)->sum('bonus');                  //剩余积分
        $this->get_machlist();
        $map['add_balance|pay_balance|del_balance'] = ['neq',0];
        $list = $CardUserUpdate->where($map)->order('id desc' )->paginate('20',false,['query' => request()->param()]);
        if ($list){
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '成功';
            $rarr['data'] = $list;
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '成功';
        }else{
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '失败';
            $rarr['data'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '失败';
        }
        return $rarr;
    }

    //充值订单详情
    public function getrecorderinfo(Request $request){
        if ($request->isAjax()){
            $param = $request->post();
            $CardUserFee = new CardUserFee();
            $map['a.orderid'] = $param['orderid'];
            $map['a.id'] = $param['id'];
//            return json($map);
            $info = $CardUserFee->alias('a')
                ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                ->join('wn_merchant c', 'a.mid=c.id', 'left')
                ->field('a.orderid,a.mid,a.code,a.num,a.source,a.cate,a.pay_cate,a.create_time,a.action_user,a.remark,b.name,b.nickname,c.merchant')
                ->where($map)->find();
            $info['pay_cate'] = getpaycate($info['pay_cate']);
            $info['cate'] = getcate($info['cate']);
            return json($info);
        }
    }



}